package com.decent.ejfadmin.user.controller;


import cn.decent.component.shiro.entity.Admin;
import cn.decent.component.shiro.service.AdminService;
import cn.decent.component.shiro.vo.AdminVO;
import com.decent.ejfadmin.common.bean.AdminInterface;
import com.decent.ejfadmin.common.bean.MessageBean;
import com.decent.ejfadmin.common.bean.Page;
import com.decent.ejfadmin.user.service.AdminInterfaceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 交接班
 *
 * @author weicx
 * @date 2018/07/26
 */
@RestController
@RequestMapping("AdminInterface")
@Slf4j
public class AdminInterfaceController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private AdminInterfaceService adminInterfaceService;

    /**
     * 新增交接班内容
     *
     * @param adminInterface 交接班实体
     * @return
     */
    @RequestMapping("contentIn")
    public MessageBean contentIn(AdminInterface adminInterface) {
        try {
            if (StringUtils.isBlank(adminInterface.getValidity())) {
                return MessageBean.fail("请选择有效期");
            }
            if (StringUtils.isBlank(adminInterface.getTitle())) {
                return MessageBean.fail("请输入标题");
            }
            if (StringUtils.isBlank(adminInterface.getContent())) {
                return MessageBean.fail("请输入内容");
            }
            if (adminInterface.getTitle().length() > 25) {
                return MessageBean.fail("标题过长");
            }
            if (StringUtils.isBlank(adminInterface.getBindRoleIds())) {
                return MessageBean.fail("请选择角色");
            }
            if (Objects.isNull(adminInterface.getType())) {
                return MessageBean.fail("请选择交接性质");
            }
            Admin admin = adminService.getCurrentAdmin();
            adminInterface.setAdminName(admin.getName());
            adminInterface.setAdminId(admin.getId());
            adminInterface.setAdminType(admin.getRoleId());
            return adminInterfaceService.contentIn(adminInterface);
        } catch (Exception e) {
            log.warn("新增交接内容异常", e);
            return MessageBean.fail();
        }

    }

    /**
     * 首页查询交接班列表
     *
     * @return
     */
    @RequestMapping("queryTitle")
    public List<AdminInterface> queryTitle() {
        try {
            Admin admin = null;
            admin = adminService.getCurrentAdmin();
            return adminInterfaceService.queryTitle(admin.getRoleId());
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }

    }

    /**
     * 标为已读
     *
     * @param id 交接id
     * @return
     */
    @RequestMapping("readerInsert")
    public MessageBean readerInsert(Integer id) {
        try {
            Admin admin = adminService.getCurrentAdmin();
            return adminInterfaceService.readerInsert(admin.getName(), id);
        } catch (Exception e) {
            log.warn("标为已读异常[{}]", e);
            return MessageBean.fail();
        }
    }

    /**
     * 分页查询交接班内容
     *
     * @param page           分页组件
     * @param startTime      开始时间
     * @param endTime        结束时间
     * @param adminInterface 交接实体
     * @return
     */
    @RequestMapping("queryTitleList")
    public Page queryTitleList(Page page, String startTime, String endTime, AdminInterface adminInterface) {
        try {
            Admin admin = adminService.getCurrentAdmin();
            adminInterface.setAdminType(admin.getRoleId());
            return adminInterfaceService.queryTitleList(page, startTime, endTime, adminInterface);
        } catch (Exception e) {
            log.warn("查询交接班内容异常[{}]", e);
            page.setQueryBean(MessageBean.fail("查询出错:("));
            page.setResultList(Collections.emptyList());
            page.setTotal(0);
            return page;
        }
    }

    /**
     * 修改交接班内容
     *
     * @param adminInterface 交接班实体
     * @return
     */
    @RequestMapping("updateContent")
    public MessageBean updateContent(AdminInterface adminInterface) {
        try {
            if (StringUtils.isBlank(adminInterface.getValidity())) {
                return MessageBean.fail("请选择有效期");
            }
            if (StringUtils.isBlank(adminInterface.getTitle())) {
                return MessageBean.fail("请输入标题");
            }
            if (StringUtils.isBlank(adminInterface.getContent())) {
                return MessageBean.fail("请输入内容");
            }
            if (adminInterface.getTitle().length() > 25) {
                return MessageBean.fail("标题过长");
            }
            if (StringUtils.isBlank(adminInterface.getBindRoleIds())) {
                return MessageBean.fail("请选择角色");
            }
            if (Objects.isNull(adminInterface.getType())) {
                return MessageBean.fail("请选择交接性质");
            }
            Admin admin = adminService.getCurrentAdmin();
            adminInterface.setUpdateAdmin(admin.getName());
            return adminInterfaceService.updateContent(adminInterface);
        } catch (Exception e) {
            log.warn("修改交接内容异常", e);
            return MessageBean.fail();
        }
    }

    /**
     * 根据id查询交接内容
     *
     * @param id 交接实体
     * @return
     */
    @RequestMapping("queryAdminByid")
    public AdminInterface queryAdminByid(Integer id) {
        return adminInterfaceService.queryAdminByid(id);

    }

    /**
     * 查询当前管理员是否有未读的交接内容
     *
     * @return
     */
    @RequestMapping("queryClose")
    public Boolean queryClose() {
        try {
            return adminInterfaceService.queryClose();
        } catch (Exception e) {
            log.warn("查询是否有未读的交接班异常[{}]", e);
            return true;
        }
    }

    /**
     * 获取当前用户
     *
     * @return
     */
    @RequestMapping("getCurrentAdmin")
    public String getCurrentAdmin() {
        try {
            Admin admin = adminService.getCurrentAdmin();
            if (admin == null) {
                return "";
            } else {
                return admin.getName();
            }
        } catch (Exception e) {
            log.warn("获取当前用户异常[{}]", e);
            return "";
        }

    }

    /**
     * 获取当前用户权限
     *
     * @return
     */
    @RequestMapping("queryAdminRole")
    public MessageBean queryAdminRole() {
        try {
            AdminVO currentAdmin = adminService.getCurrentAdmin();
            if (Objects.nonNull(currentAdmin.getSuperAdmin()) && currentAdmin.getSuperAdmin()) {
                return MessageBean.success();
            }
        } catch (Exception e) {
            log.warn("[queryAdminRole]异常{}", e, e);
        }
        return MessageBean.fail();
    }

    /**
     * 修改密码
     *
     * @param oldPwd 旧密码
     * @param newPwd 新密码
     * @return
     * @throws Exception
     */
    @RequestMapping("modifyPwd")
    public MessageBean modifyPwd(String oldPwd, String newPwd) {
        try {
            if (StringUtils.isAnyBlank(oldPwd, newPwd)) {
                return MessageBean.fail("原密码或新密码不能为空");
            }
            return adminInterfaceService.modifyAdminPwd(oldPwd, newPwd);
        } catch (Exception e) {
            return MessageBean.fail();
        }
    }


    /**
     * 重置用户登录密码
     *
     * @param phoneNo 手机号码
     * @returnqueryTitleList
     */
    @RequestMapping("resetPwd")
    public MessageBean resetUserLoginPwd(String phoneNo) {
        if (StringUtils.isBlank(phoneNo)) {
            return new MessageBean(false, "请选择要修改的用户", false);
        }
        try {
            String pwd = adminInterfaceService.resetUserLoginPwd(phoneNo);
            return new MessageBean(true, pwd, null);
        } catch (Exception ex) {
            return new MessageBean(ex);
        }
    }


}
